fn f1(x: f64) -> f64 {
    if x < 0.6 {
        x + 2.0 * (1.0 - x) * (x + 0.08)
            + 3.0 * (1.0 - x) * (0.92 - x) * (x + 0.16)
            + 4.0 * (1.0 - x) * (0.92 - x) * (0.84 - x) * (x + 0.24)
            + 5.0 * (1.0 - x) * (0.92 - x) * (0.84 - x) * (0.76 - x) * (x + 0.32)
            + 6.0 * (1.0 - x) * (0.92 - x) * (0.84 - x) * (0.76 - x) * (0.68 - x) * (x + 0.4)
            + (7.0 + (0.6 - x) / (0.4 + x)) * (1.0 - x) * (0.92 - x) * (0.84 - x) * (0.76 - x) * (0.68 - x) * (0.6 - x)
    } else if x < 0.68 {
        x + 2.0 * (1.0 - x) * (x + 0.08)
            + 3.0 * (1.0 - x) * (0.92 - x) * (x + 0.16)
            + 4.0 * (1.0 - x) * (0.92 - x) * (0.84 - x) * (x + 0.24)
            + 5.0 * (1.0 - x) * (0.92 - x) * (0.84 - x) * (0.76 - x) * (x + 0.32)
            + 6.0 * (1.0 - x) * (0.92 - x) * (0.84 - x) * (0.76 - x) * (0.68 - x)
    } else if x < 0.76 {
        x + 2.0 * (1.0 - x) * (x + 0.08)
            + 3.0 * (1.0 - x) * (0.92 - x) * (x + 0.16)
            + 4.0 * (1.0 - x) * (0.92 - x) * (0.84 - x) * (x + 0.24)
            + 5.0 * (1.0 - x) * (0.92 - x) * (0.84 - x) * (0.76 - x)
    } else if x < 0.84 {
        x + 2.0 * (1.0 - x) * (x + 0.08)
            + 3.0 * (1.0 - x) * (0.92 - x) * (x + 0.16)
            + 4.0 * (1.0 - x) * (0.92 - x) * (0.84 - x)
    } else if x < 0.92 {
        x + 2.0 * (1.0 - x) * (x + 0.08)
            + 3.0 * (1.0 - x) * (0.92 - x)
    } else {
        x + 2.0 * (1.0 - x)
    }
}

fn f2(x: f64) -> f64 {
    if x < 0.5 {
        x + 2.0 * (1.0 - x) * (x + 0.1)
            + 3.0 * (1.0 - x) * (0.9 - x) * (x + 0.2)
            + 4.0 * (1.0 - x) * (0.9 - x) * (0.8 - x) * (x + 0.3)
            + 5.0 * (1.0 - x) * (0.9 - x) * (0.8 - x) * (0.7 - x) * (x + 0.4)
            + 6.0 * (1.0 - x) * (0.9 - x) * (0.8 - x) * (0.7 - x) * (0.6 - x) * (x + 0.5)
            + (7.0 + (0.5 - x) / (0.5 + x)) * (1.0 - x) * (0.9 - x) * (0.8 - x) * (0.7 - x) * (0.6 - x) * (0.5 - x)
    } else if x < 0.6 {
        x + 2.0 * (1.0 - x) * (x + 0.1)
            + 3.0 * (1.0 - x) * (0.9 - x) * (x + 0.2)
            + 4.0 * (1.0 - x) * (0.9 - x) * (0.8 - x) * (x + 0.3)
            + 5.0 * (1.0 - x) * (0.9 - x) * (0.8 - x) * (0.7 - x) * (x + 0.4)
            + 6.0 * (1.0 - x) * (0.9 - x) * (0.8 - x) * (0.7 - x) * (0.6 - x)
    } else if x < 0.7 {
        x + 2.0 * (1.0 - x) * (x + 0.1)
            + 3.0 * (1.0 - x) * (0.9 - x) * (x + 0.2)
            + 4.0 * (1.0 - x) * (0.9 - x) * (0.8 - x) * (x + 0.3)
            + 5.0 * (1.0 - x) * (0.9 - x) * (0.8 - x) * (0.7 - x)
    } else if x < 0.8 {
        x + 2.0 * (1.0 - x) * (x + 0.1)
            + 3.0 * (1.0 - x) * (0.9 - x) * (x + 0.2)
            + 4.0 * (1.0 - x) * (0.9 - x) * (0.8 - x)
    } else if x < 0.9 {
        x + 2.0 * (1.0 - x) * (x + 0.1)
            + 3.0 * (1.0 - x) * (0.9 - x)
    } else {
        x + 2.0 * (1.0 - x)
    }
}

fn f3(x: f64) -> f64 {
    if x < 0.4 {
        x + 2.0 * (1.0 - x) * (x + 0.12)
            + 3.0 * (1.0 - x) * (0.88 - x) * (x + 0.24)
            + 4.0 * (1.0 - x) * (0.88 - x) * (0.76 - x) * (x + 0.36)
            + 5.0 * (1.0 - x) * (0.88 - x) * (0.76 - x) * (0.64 - x) * (x + 0.48)
            + 6.0 * (1.0 - x) * (0.88 - x) * (0.76 - x) * (0.64 - x) * (0.52 - x) * (x + 0.6)
            + (7.0 + (0.4 - x) / (0.6 + x)) * (1.0 - x) * (0.88 - x) * (0.76 - x) * (0.64 - x) * (0.52 - x) * (0.4 - x)
    } else if x < 0.52 {
        x + 2.0 * (1.0 - x) * (x + 0.12)
            + 3.0 * (1.0 - x) * (0.88 - x) * (x + 0.24)
            + 4.0 * (1.0 - x) * (0.88 - x) * (0.76 - x) * (x + 0.36)
            + 5.0 * (1.0 - x) * (0.88 - x) * (0.76 - x) * (0.64 - x) * (x + 0.48)
            + 6.0 * (1.0 - x) * (0.88 - x) * (0.76 - x) * (0.64 - x) * (0.52 - x)
    } else if x < 0.64 {
        x + 2.0 * (1.0 - x) * (x + 0.12)
            + 3.0 * (1.0 - x) * (0.88 - x) * (x + 0.24)
            + 4.0 * (1.0 - x) * (0.88 - x) * (0.76 - x) * (x + 0.36)
            + 5.0 * (1.0 - x) * (0.88 - x) * (0.76 - x) * (0.64 - x)
    } else if x < 0.76 {
        x + 2.0 * (1.0 - x) * (x + 0.12)
            + 3.0 * (1.0 - x) * (0.88 - x) * (x + 0.24)
            + 4.0 * (1.0 - x) * (0.88 - x) * (0.76 - x)
    } else if x < 0.88 {
        x + 2.0 * (1.0 - x) * (x + 0.12)
            + 3.0 * (1.0 - x) * (0.88 - x)
    } else {
        x + 2.0 * (1.0 - x)
    }
}

fn f4(x: f64) -> f64 {
    if x < 0.3 {
        x + 2.0 * (1.0 - x) * (x + 0.14)
            + 3.0 * (1.0 - x) * (0.86 - x) * (x + 0.28)
            + 4.0 * (1.0 - x) * (0.86 - x) * (0.72 - x) * (x + 0.42)
            + 5.0 * (1.0 - x) * (0.86 - x) * (0.72 - x) * (0.58 - x) * (x + 0.56)
            + 6.0 * (1.0 - x) * (0.86 - x) * (0.72 - x) * (0.58 - x) * (0.44 - x) * (x + 0.7)
            + (7.0 + (0.3 - x) / (0.7 + x)) * (1.0 - x) * (0.86 - x) * (0.72 - x) * (0.58 - x) * (0.44 - x) * (0.3 - x)
    } else if x < 0.44 {
        x + 2.0 * (1.0 - x) * (x + 0.14)
            + 3.0 * (1.0 - x) * (0.86 - x) * (x + 0.28)
            + 4.0 * (1.0 - x) * (0.86 - x) * (0.72 - x) * (x + 0.42)
            + 5.0 * (1.0 - x) * (0.86 - x) * (0.72 - x) * (0.58 - x) * (x + 0.56)
            + 6.0 * (1.0 - x) * (0.86 - x) * (0.72 - x) * (0.58 - x) * (0.44 - x)
    } else if x < 0.58 {
        x + 2.0 * (1.0 - x) * (x + 0.14)
            + 3.0 * (1.0 - x) * (0.86 - x) * (x + 0.28)
            + 4.0 * (1.0 - x) * (0.86 - x) * (0.72 - x) * (x + 0.42)
            + 5.0 * (1.0 - x) * (0.86 - x) * (0.72 - x) * (0.58 - x)
    } else if x < 0.72 {
        x + 2.0 * (1.0 - x) * (x + 0.14)
            + 3.0 * (1.0 - x) * (0.86 - x) * (x + 0.28)
            + 4.0 * (1.0 - x) * (0.86 - x) * (0.72 - x)
    } else if x < 0.86 {
        x + 2.0 * (1.0 - x) * (x + 0.14)
            + 3.0 * (1.0 - x) * (0.86 - x)
    } else {
        x + 2.0 * (1.0 - x)
    }
}

fn f5(x: f64) -> f64 {
    if x < 0.2 {
        x + 2.0 * (1.0 - x) * (x + 0.16)
            + 3.0 * (1.0 - x) * (0.84 - x) * (x + 0.32)
            + 4.0 * (1.0 - x) * (0.84 - x) * (0.68 - x) * (x + 0.48)
            + 5.0 * (1.0 - x) * (0.84 - x) * (0.68 - x) * (0.52 - x) * (x + 0.64)
            + 6.0 * (1.0 - x) * (0.84 - x) * (0.68 - x) * (0.52 - x) * (0.36 - x) * (x + 0.8)
            + (7.0 + (0.2 - x) / (0.8 + x)) * (1.0 - x) * (0.84 - x) * (0.68 - x) * (0.52 - x) * (0.36 - x) * (0.2 - x)
    } else if x < 0.36 {
        x + 2.0 * (1.0 - x) * (x + 0.16)
            + 3.0 * (1.0 - x) * (0.84 - x) * (x + 0.32)
            + 4.0 * (1.0 - x) * (0.84 - x) * (0.68 - x) * (x + 0.48)
            + 5.0 * (1.0 - x) * (0.84 - x) * (0.68 - x) * (0.52 - x) * (x + 0.64)
            + 6.0 * (1.0 - x) * (0.84 - x) * (0.68 - x) * (0.52 - x) * (0.36 - x)
    } else if x < 0.52 {
        x + 2.0 * (1.0 - x) * (x + 0.16)
            + 3.0 * (1.0 - x) * (0.84 - x) * (x + 0.32)
            + 4.0 * (1.0 - x) * (0.84 - x) * (0.68 - x) * (x + 0.48)
            + 5.0 * (1.0 - x) * (0.84 - x) * (0.68 - x) * (0.52 - x)
    } else if x < 0.68 {
        x + 2.0 * (1.0 - x) * (x + 0.16)
            + 3.0 * (1.0 - x) * (0.84 - x) * (x + 0.32)
            + 4.0 * (1.0 - x) * (0.84 - x) * (0.68 - x)
    } else if x < 0.84 {
        x + 2.0 * (1.0 - x) * (x + 0.16)
            + 3.0 * (1.0 - x) * (0.84 - x)
    } else {
        x + 2.0 * (1.0 - x)
    }
}

pub fn royal_series_critical_bonus(refine: usize, x: f64) -> f64 {
    if refine == 1 {
        1.0 / f1(x) - x
    } else if refine == 2 {
        1.0 / f2(x) - x
    } else if refine == 3 {
        1.0 / f3(x) - x
    } else if refine == 4 {
        1.0 / f4(x) - x
    } else {
        1.0 / f5(x) - x
    }
}
